package tjs.styudy.mini.springboot.devtools.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tjs.styudy.mini.springboot.devtools.fileWatch.ClassPathChangedEvent;
import tjs.styudy.mini.springboot.devtools.fileWatch.FileSystemWatcher;

/**
 * Created by Nicky.Tang on 2021/10/16 2:55 下午
 *
 * @since 02.12.10
 */
@Configuration
public class RestartConfiguration {

    @Bean
    public ApplicationListener<ClassPathChangedEvent> restartingClassPathChangedEventListener() {
        return (event) -> {
            if (event.isRestartRequired()) {
                Restarter.getInstance().restart(event);
            }
        };
    }

    @Bean
    @ConditionalOnMissingBean
    public FileSystemWatcher fileSystemWatcher() {
        return new FileSystemWatcher(Restarter.getInstance().getInitialUrls());
    }

}
